from google.appengine.ext import db

class User(db.Model):
    user = db.UserProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True)

class Channel(db.Model):
    link = db.LinkProperty(required=True)
    title = db.StringProperty()
    fetch_interval = db.TimeProperty()
    last_fetch_try = db.DateTimeProperty()
    last_fetch_suc = db.DateTimeProperty()
    last_new_article = db.DateTimeProperty()

# Parent is channel
class Item(db.Model):
    title = db.StringProperty()
    link = db.LinkProperty()
    content = db.TextProperty()
    pubDate = db.DateTimeProperty()
    fetch_time = db.DateTimeProperty(auto_now_add=True)

# Parent is user
class Tag(db.Model):
    name = db.StringProperty()

# Parent is user
class Subscribe(db.Model):
    channel = db.ReferenceProperty(reference_class=Channel, collection_name='subscribe_set', required=True)
    tag = db.ReferenceProperty(reference_class=Tag, collection_name='subscribe_set')
    add_time = db.DateTimeProperty(auto_now_add=True)

# Parent is user
class ReadedItem(db.Model):
    channel = db.ReferenceProperty(reference_class=Channel, collection_name='readed_item_set', required=True)
    item = db.ReferenceProperty(reference_class=Item, collection_name='readed_item_set', required=True)
    read_time = db.DateTimeProperty(auto_now_add=True)

# Parent is user
class StarItem(db.Model):
    channel = db.ReferenceProperty(reference_class=Channel, collection_name='star_item_set', required=True)
    item = db.ReferenceProperty(reference_class=Item, collection_name='star_item_set', required=True)
    add_time = db.DateTimeProperty(auto_now_add=True)

# Parent is user
class UnreadItem(db.Model):
    channel = db.ReferenceProperty(reference_class=Channel, collection_name='unread_item_set', required=True)
    item = db.ReferenceProperty(reference_class=Item, collection_name='unread_item_set', required=True)
    add_time = db.DateTimeProperty(auto_now_add=True)

# Parent is user
class Maggic(db.Model):
    name = db.StringProperty()
    invalid = db.BooleanProperty()
    maggic_id = db.IntegerProperty()
    generate_time = db.DateTimeProperty()
    items = db.ListProperty(db.Key)
    offset = db.IntegerProperty()
    total = db.IntegerProperty()

# maggic unread-list, stored in flask.g as global variables.
class UnreadList:
    userEmail = ''
    created_time = 0    #datetime.datetime
    magic = ''
    listLenth = 0
    cur = 0
    items = []